package de.matusystems

import grails.converters.JSON


class FileController {

	def s3Service
	URLEncoder urlEncoder = new URLEncoder()

	def get() {

		def key = urlEncoder.encode(params['id'], "UTF-8")
		def fileMap = s3Service.getFile(key)

		response.setContentType(fileMap['contentType'])
		response.outputStream.write(fileMap['file'])
	}

	def save() {

		def key = s3Service.storeFile(params['filename'], params['contentType'], params['file'].decodeBase64())

		def json = [:]
		json['key'] = key
		json['link'] = System.getenv()['CAPP_STORAGE_SERVICE']+"/file/"+key
		render json as JSON
	}

	def delete() {

		s3Service.deleteFile(id)
	}
}
